﻿namespace Eneta.Portal.Events
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Publishing;
    using Eneta.Portal.Common.Helpers;
    using Microsoft.SharePoint.Utilities;
    using System.Web.UI.HtmlControls;

    public class NewEventPage : Page
    {
        #region Controls
        protected Button btnSave;
        protected TextBox tbTitle;
        protected TextBox tbPlace;
        protected TextBox tbShortDesc;
        protected TextBox tbPlacesCount;
        protected DropDownList ddlTechnicalLevel;
        #endregion Controls

        private SPWeb web;
        private SPContentType ct;
        private Dictionary<string, string> errors = new Dictionary<string, string>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnSave.Click += new EventHandler(BtnSave_Click);

            errors.Clear();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;

            ct = web.AvailableContentTypes["Event"];

            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            SPFieldChoice fldTechnicalLevel = (SPFieldChoice)ct.Fields.GetFieldByInternalName("TechnicalLevel");

            ddlTechnicalLevel.Items.Add(string.Empty);

            foreach (string curChoice in fldTechnicalLevel.Choices)
            {
                ddlTechnicalLevel.Items.Add(curChoice);
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbTitle.Text))
            {
                errors.Add("title", "Nimi on kohustuslik väli.");
            }

            if (string.IsNullOrEmpty(tbPlace.Text))
            {
                errors.Add("place", "Koht on kohustuslik väli.");
            }

            if (string.IsNullOrEmpty(tbShortDesc.Text))
            {
                errors.Add("shortDesc", "Lühikirjeldus on kohustuslik väli.");
            }

            int placesCount;
            if (!string.IsNullOrEmpty(tbPlacesCount.Text) && !int.TryParse(tbPlacesCount.Text, out placesCount))
            {
                errors.Add("placesCount", "Kohtade arv ei ole korrektselt sisestatud.");
            }

            if (errors.Count > 0)
            {
                ApplyErrors();
                return;
            }

            string pageTitle = tbTitle.Text;

            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
            PageLayout layoutEvent = publishingWeb.GetAvailablePageLayouts(ct.Id)[0];
            PublishingPageCollection pages = publishingWeb.GetPublishingPages();
            PublishingPage publishingPage = pages.Add(UrlHelper.GetPageFileName(pageTitle), layoutEvent);

            SPListItem item = publishingPage.ListItem;
            item[SPBuiltInFieldId.Title] = tbTitle.Text;
            item["Place"] = tbPlace.Text;
            item["ShortDescription"] = tbShortDesc.Text;
            item["TechnicalLevel"] = ddlTechnicalLevel.Text;
            item["PlacesCount"] = tbPlacesCount.Text;
            item.SystemUpdate(false);

            SPUtility.Redirect("/Default.aspx", SPRedirectFlags.Default, Context);
        }

        private void ApplyErrors()
        {
            foreach (string key in errors.Keys)
            {
                HtmlGenericControl ctrl = this.FindControl(key + "Container", true) as HtmlGenericControl;
                if (ctrl != null)
                {
                    ctrl.Attributes.Add("class", "info-row failed-info");
                    
                    ctrl = this.FindControl(key + "ErrorMessage", true) as HtmlGenericControl;
                    if (ctrl != null)
                    {
                        ctrl.Style["display"] = string.Empty;
                        ctrl = this.FindControl(key + "ErrorLabel", true) as HtmlGenericControl;
                        if (ctrl != null)
                        {
                            ctrl.InnerText = errors[key];
                        }
                    }
                }
            }
        }
    }
}
